<?php

namespace app\admin\model;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use support\Model;

/**
 * 菜单模型
 * @property int $id
 * @property int $parent_id 父级ID
 * @property string $name 菜单名称
 * @property string|null $path 路由路径
 * @property string|null $component 组件路径
 * @property string|null $icon 图标
 * @property string|null $permission 权限标识
 * @property string $type 类型:menu=菜单,button=按钮,api=接口
 * @property string|null $method 请求方法
 * @property int $status 状态:0=隐藏,1=显示
 * @property int $sort 排序
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class Menu extends Model
{
    /**
     * The table associated with the model.
     * @var string
     */
    protected $table = 'menu';
    /**
     * The primary key associated with the table.
     * @var string
     */
    protected $primaryKey = 'id';
    /**
     * Indicates if the model should be timestamped.
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     * @var string|null
     */
    const CREATED_AT = 'create_time';
    /**
     * The name of the "updated at" column.
     * @var string|null
     */
    const UPDATED_AT = 'update_time';
    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = [
        'parent_id',
        'name',
        'path',
        'component',
        'icon',
        'permission',
        'type',
        'method',
        'status',
        'sort',
    ];
    /**
     * The attributes that should be cast to native types.
     * @var array
     */
    protected $casts = [
        'parent_id' => 'integer',
        'status' => 'integer',
        'sort' => 'integer',
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s',
    ];

    /**
     * 获取拥有此菜单的角色
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class, 'role_menu', 'menu_id', 'role_id');
    }
}
